<template>
  <Modal v-model:visible="visible" title="续订应用" @cancel="close" :destroy-on-close="true">
    <template #footer>
      <Button key="submit" type="primary" @click="submit">确认</Button>
      <Button key="back" @click="close">取消</Button>
    </template>
    <Form :model="formState" v-bind="formItemLayout" ref="formRef">
      <FormItem label="到期时间" name="expireTime" :rules="[{ required: true, message: '请选择到期时间' }]">
        <DatePicker
          v-model:value="formState.expireTime"
          placeholder="请选择"
          :disabled-date="disabledDate"
          :showToday="false"
          :defaultPickerValue="dayjs(originExpireTime)"
        />
      </FormItem>
    </Form>
  </Modal>
</template>
<script lang="ts" setup>
import { Modal, Form, DatePicker, FormItem, message, Button } from 'ant-design-vue';
import { ref } from 'vue';
import { renewApp } from '/@/api/tenant-mgmt/enterpriseAppManagement/enterpriseAppManagement';
import { RenewParam } from '/@/api/tenant-mgmt/enterpriseAppManagement/model/enterpriseAppManagementModel';
import dayjs, { Dayjs } from 'dayjs';
const visible = ref(false);
let formState = ref({} as RenewParam);
const originExpireTime = ref();
const openRenew = (record) => {
  formState.value.appId = record.appId;
  formState.value.tenantCode = record.tenantCode;
  originExpireTime.value = record.expireTime;
  formState.value.packageId = record.packageId;
  visible.value = true;
};

const disabledDate = (current: Dayjs) => {
  return current < dayjs(originExpireTime.value).endOf('day') || current > dayjs('9999-12-31 23:59:59');
};
const emit = defineEmits(['submitSuccess']);
const close = () => {
  visible.value = false;
  formState.value = {} as RenewParam;
};
const formRef = ref();
const submit = async () => {
  let param = {};
  formRef.value.validate().then(async () => {
    let expireTime = dayjs(formState.value.expireTime).endOf('date').format('YYYY-MM-DD HH:mm:ss');
    param = {
      appId: formState.value.appId,
      tenantCode: formState.value.tenantCode,
      expireTime,
      packageId: formState.value.packageId,
    };
    let res = await renewApp(param);
    if (res) {
      message.success(res);
    }
    emit('submitSuccess');
    close();
  });
};

const formItemLayout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 14 },
};
defineExpose({ openRenew });
</script>
